<?php

namespace Home\Controller;
use Think\Controller;
class SayController extends HController {
    public function index(){
        if (empty($_GET['user_id'])) {
            $vuid = $_SESSION['qzone']['home']['id'];
        } else {
            $vuid = $_GET['user_id'];//被访问用户id
        }
        $uid = $_SESSION['qzone']['home']['id'];//当前用户id
        $user_model = M('User');
        $user = $user_model->find($uid);
        $vuser = $user_model->find($vuid);
        
        $sayModel = D('Say');
        $saylist = $sayModel->getlist($vuid);

        $this->assign('saylist',$saylist['data']);// 赋值数据集
        $this->assign('page',$saylist['page']);// 赋值分页输出
        $this->assign('say_count',$saylist['say_count']);// 赋值分页输出
        $this->assign('user',$user);// 赋值当前用户信息输出
        $this->assign('vuser',$vuser);// 赋值访问用户信息输出
        $this->display();
    }

    //说说评论回复
    public function comhf(){
        if (IS_AJAX){
            $uid =$_SESSION['qzone']['home']['id'];
            $say_comment_huif['com_id'] = I('post.com_id');
            $say_comment_huif['content'] = I('post.content');
            $say_comment_huif['hf_id'] = I('post.hfid');
            $say_comment_huif['uid'] = $uid;
            $say_comment_huif['ctime'] = time();

            $model = M('Say_comment_huif');
            $model->create();

            $res = $model->add($say_comment_huif);
            $id = $res;

            if($res){
                $user_model = M('User');
                $user1 = $user_model->find($uid);
                $user2 = $user_model->find($say_comment_huif['hf_id']);
                $data['zonedp1'] = $user1['zonedp'];
                $data['name1'] = $user1['nickname'];
                $data['name2'] = $user2['nickname'];
                $data['status'] = 'success';
                $data['msg'] = '发表成功！';
                $data['id'] = $res;
                $data['url1'] = U('/Home/Index/index',array('user_id'=>$uid));
                $data['url2'] = U('/Home/Index/index',array('user_id'=>$user2['id']));
                $data['content'] = $say_comment_huif['content'];
                $data['ctime'] = date('Y-m-d H:i:s',$say_comment_huif['ctime']);
                $this->ajaxReturn($data);
            } else {
                $data['status'] = 'failed';
                $data['msg'] = '发表失败，请重试！';
                $this->ajaxReturn($data);
            } 
        }else {
            $data['status'] = 'failed';
            $data['msg'] = '非法请求';
            $this->ajaxReturn($data);
        }
    }

    public function edit() {
        $id = I('get.id');
        //判断说说作者
        $check_model = D('Say');
        $res = $check_model->check_auth($id,'Say');

        if(!$res){
            $this->error('没有编辑权限');
            exit;
        }

        $model = M('say');
        $pic_mdl = M('say_photo');

        $say = $model->where(array('id'=>$id,'state'=>0))->find();
        if(empty($say)){
            $this->error('无效请求');
        }
        $say_pic = $pic_mdl->where(array('say_id'=>$id))->select();
        if(!empty($say_pic)){
            $say['pic'] = $say_pic;
        }
        $uid = $_SESSION['qzone']['home']['id'];
        $user_model = M('User');
        $user = $user_model->find($uid);
        $this->assign('vuser',$user);// 赋值用户信息输出
        $this->assign('say',$say);
        $this->display();
    }

    public function toedit(){
        if(IS_POST){
            $uid = $_SESSION['qzone']['home']['id'];
            $data['id'] = I('post.say_id');
            $data['content'] = I('post.content');

            $newimg = I('img');
            $model = M('say');
            $photo_mdl = M('say_photo');
            $res = $model->save($data);

            foreach($newimg as $k => $v){
                $imgdata['uid'] = $uid;
                $imgdata['path'] = $v;
                $imgdata['say_id'] = I('post.say_id');
                $imgdata['ctime'] = time();
                $photo_mdl->create();
                $photo_mdl->add($imgdata);
            }

            $this->success('编辑说说成功',U('index'));
        }
    }

    public function uploadimg(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/uploads/'; // 设置附件上传根目录
        $upload->subName = array('date','Ymd'); // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();

        if(!$info) {// 上传错误提示错误信息
            $data['status'] = 'failed';
            $data['msg'] = $upload->getError();
            $this->ajaxReturn($data);
        }else{// 上传成功
            $data['status'] = 'success';
            $data['info'] = $info;
            $this->ajaxReturn($data);
        }
    }

    //保存说说
    public function tosave(){
        if(IS_AJAX){
            $uid =$_SESSION['qzone']['home']['id'];
            $say['content'] = I('post.content');
            $img = I('post.img');
            $say['uid'] = $uid;
            $say['ctime'] = time();



            $model = M('say');
            $model->create();       
            $res = $model->add($say);

            if(!empty($img)){
                $photo_model = M('say_photo');
                $pic['uid'] = $uid;
                $pic['say_id'] = $res;
                $pic['ctime'] = time();
                $tp = '';
                foreach($img as $k=>$v){
                    $pic['path'] = $v;
                    $photo_model->create();
                    $photo_model->add($pic);
                    //保存前三张图片
                    $tp .= "<img src='/qzone/Public/uploads/{$v}'/>";
                }
                
            }
            //说说发表成功，添加动态
            $DModel = M('Dynamic');
            $DData['uid'] = $uid;
            $DData['username'] = $_SESSION['qzone']['home']['username'];
            $DData['zonedp'] = $_SESSION['qzone']['home']['zonedp'];
            //日志的动态内容格式是标题加部分内容
            $DData['content'] = "<p>{$say['content']}</p>".$tp;
            $DData['is_show'] = 1;
            $DData['ctime'] = time();
            $DModel->create();
            $Did = $DModel->add($DData);
            //如果是通过首页提交过来的说说的话，返回的结果与说说页面不同
            $source = I('post.source');
            if($source == 'infocent')
            {
                $this->ajaxReturn(array('status'=>0,'ctime'=>date('Y-m-d H:i',$DData['ctime']),'username'=>$DData['username'],'zonedp'=>$DData['zonedp'],'content'=>$DData['content'],'id'=>$Did));
            }
            if($res){
                $user_model = M('user');
                $user = $user_model->find($uid);
                $data['zonedp'] = $user['zonedp'];
                $data['name'] = $user['nickname'];
                $data['status'] = 'success';
                $data['msg'] = '发表成功！';
                $data['id'] = $res;
                $data['url'] = U('edit',array('user_id'=>$this->myUserData['id'],'id'=>$res));
                $data['url2'] = U('Home/Index/index',array('user_id'=>$uid));
                $data['content'] = $say['content'];
                $data['ctime'] = date('Y-m-d H:i:s',$say['ctime']);
                $this->ajaxReturn($data);
            } else {
                $data['status'] = 'failed';
                $data['msg'] = '发表失败，请重试！';
                $this->ajaxReturn($data);
            }
            
        } else {
            $data['status'] = 'failed';
            $data['msg'] = '非法请求';
            $this->ajaxReturn($data);
        }
    }

    //保存说说评论
    public function tosavecomment(){
        if(IS_AJAX){
            $uid =$_SESSION['qzone']['home']['id'];
            $say_comment['content'] = I('post.content');
            $say_comment['who_id'] = I('post.who_id');
            $say_comment['uid'] = $uid;
            $say_comment['ctime'] = time();
            $model = M('Say_comment');
            $model->create();       
            $res = $model->add($say_comment);

            if($res){
                $user_model = M('user');
                $user = $user_model->find($uid);
                $data['zonedp'] = $user['zonedp'];
                $data['name'] = $user['nickname'];
                $data['uid'] = $user['id'];
                $data['url'] = U('Home/Index/index',array('user_id'=>$uid));
                $data['status'] = 'success';
                $data['msg'] = '发表成功！';
                $data['id'] = $res;
                $data['content'] = $say_comment['content'];
                $data['ctime'] = date('Y-m-d H:i:s',$say_comment['ctime']);
                $this->ajaxReturn($data);
            } else {
                $data['status'] = 'failed';
                $data['msg'] = '发表失败，请重试！';
                $this->ajaxReturn($data);
            }
        } else {
            $data['status'] = 'failed';
            $data['msg'] = '非法请求';
            $this->ajaxReturn($data);
            }
    }
    //删除说说
    public function del(){
        if(IS_AJAX){
            $id = I('post.id');
            //判断说说作者
            $check_model = D('Say');
            $res = $check_model->check_auth($id,'Say');

            if(!$res){
                $data['status'] = 'failed';
                $data['msg'] = '没有删除权限';
                $this->ajaxReturn($data);
                exit;
            }
            //删除说说
            $model = M('say');
            $res1 = $model->delete($id);
            //删除说说评论回复
            $commodel = M('say_comment');
            $comhfmodel = M('say_comment_huif');
            $comlist = $commodel->where(array('who_id'=>$id))->select();
            foreach ($comlist as $key => $val) {
                $comhfmodel->where(array('com_id'=>$val['id']))->delete();
            }
            //删除说说评论
            $res2 = $commodel->where(array('who_id'=>$id))->delete();
            //删除说说图片
            $photo_mdl = M('say_photo');
            $photolist = $photo_mdl->where(array('say_id'=>$id))->select();
            foreach($photolist as $k=>$v){
                unlink('./Public/uploads/'.$v['path']);
                $photo_mdl->delete($v['id']);
            }

            $data['status'] = 'success';
            $data['msg'] = '删除成功';
            $this->ajaxReturn($data);

        } else {
            echo '来过';
        }
    }
    //删除说说评论
    public function delcom(){
        if(IS_AJAX){
            $id = I('post.id');
            //判断删除权限
            $check_model = D('Say');
            $res = $check_model->com_auth($id);

            if(!$res){
                $data['status'] = 'failed';
                $data['msg'] = '没有删除权限';
                $this->ajaxReturn($data);
                exit;
            }
            
            $model = M('say_comment');
            $res = $model->delete($id);

            $hfmodel = M('say_comment_huif');
            $hfmodel->where(array('com_id'=>$id))->delete();

            $data['status'] = 'success';
            $data['msg'] = '删除评论成功';
            $this->ajaxReturn($data);

        } else {
            echo '来过';
        }
    }
    //删除对评论的回复
    public function delhf(){
        if (IS_AJAX) {
            $id = I('post.id');
            //判断删除权限
            $check_model = D('Say');
            $res = $check_model->com_hf_auth($id);

            if(!$res){
                $data['status'] = 'failed';
                $data['msg'] = '没有删除权限';
                $this->ajaxReturn($data);
                exit;
            }

            $hf_model = M('Say_comment_huif');
            $res = $hf_model->delete($id);
            if ($res) {
                $data['status'] = 'success';
                $data['msg'] = '删除成功';
                $this->ajaxReturn($data);
            } else {
                $data['status'] = 'failed';
                $data['msg'] = '删除失败';
                $this->ajaxReturn($data);
            }
        } else {
            $this->error('非法请求');
        }
    }

    //删除说说图片
    public function delpic(){
        if (IS_AJAX) {
            $picname = I('post.picname');
            if(unlink('./Public/uploads/'.$picname)){
                $data['status'] = 'success';
                $data['msg'] = '删除图片成功';
                $this->ajaxReturn($data);
            } else{
                $data['status'] = 'failed';
                $data['msg'] = '删除图片失败';
                $this->ajaxReturn($data);
            }
        }
    }

    //点赞的ajax实现
    //点赞和删除同时实现，并且返回了该条说说的点赞数量
    public function fav(){
        if (IS_AJAX) {
            $id = I('post.id');
            $uid = $_SESSION['qzone']['home']['id'];

            $data['uid'] = $uid;
            $data['say_id'] = $id;

            $model=M('say_good');
            $res = $model->where($data)->find();
            $num = $model->where(array('say_id'=>$id))->count();
            if($res){
                $rtn = $model->delete($res['id']);
                if ($rtn) {
                    $info['status'] = 'success';
                    $info['msg'] = '取消赞成功';
                    $info['btn'] = '赞(' . ($num-1) . ')';
                    $info['num'] = $num-1;
                    $this->ajaxReturn($info);
                } else {
                    $info['status'] = 'failed';
                    $info['msg'] = '取消失败';
                    $info['num'] = $num;
                    $this->ajaxReturn($info);
                }
            } else{
                $rtn = $model->add($data);
                if($rtn){
                    $info['status'] = 'success';
                    $info['msg'] = '点赞成功';
                    $info['btn'] = '取消赞';
                    $info['num'] = $num+1;
                    $this->ajaxReturn($info);
                } else{
                    $info['status'] = 'failed';
                    $info['msg'] = '点赞失败';
                    $info['num'] = $num;
                    $this->ajaxReturn($info);
                }
            }
        }
    }
}
